package com.whfc.openapi.api.fvs.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whfc.common.constant.QueueConst;
import com.whfc.common.util.JSONUtil;
import com.whfc.fim.dto.FimResultWarnDTO;
import com.whfc.fim.param.FimAlgResultParam;
import com.whfc.fvs.dto.AliyunStreamInfoDTO;
import com.whfc.fvs.dto.FvsDeviceDTO;
import com.whfc.fvs.dto.FvsRecordDTO;
import com.whfc.fvs.enums.FvsCallbackStatus;
import com.whfc.fvs.enums.FvsCallbackType;
import com.whfc.fvs.enums.FvsDeviceStatus;
import com.whfc.fvs.enums.FvsStreamStatus;
import com.whfc.fvs.param.BetvDtoData;
import com.whfc.fvs.param.FvsGroupCallbackParam;
import com.whfc.fvs.param.FvsRecordCallbackParam;
import com.whfc.fvs.param.FvsSnapshotCallbackParam;
import com.whfc.fvs.service.FvsDeviceService;
import com.whfc.fvs.service.FvsRecordService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 回调
 *
 * @author guodong_sun
 * @Description:
 * @date 2019年10月28日
 */
@Service
public class FvsCallbackServiceImpl implements FvsCallbackService {

    @DubboReference(interfaceClass = FvsDeviceService.class, version = "1.0.0")
    private FvsDeviceService fvsDeviceService;

    @DubboReference(interfaceClass = FvsRecordService.class, version = "1.0.0")
    private FvsRecordService fvsRecordService;

    private static final String ossFvsUrl = "https://fvs-sh.whfciot.com";

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 空间状态改变回调
     */
    @Override
    public void groupCallback(String body) {
        FvsGroupCallbackParam fvsGroupCallbackParam = JSON.parseObject(body, FvsGroupCallbackParam.class);
        String deviceStatus = fvsGroupCallbackParam.getDeviceStatus();
        String deviceId = fvsGroupCallbackParam.getDeviceId();
        String event = fvsGroupCallbackParam.getEvent();
        Integer deviceState = null;
        if (FvsCallbackType.DEVICE.value().equals(event)) {
            if (FvsCallbackStatus.ONLINE.value().equals(deviceStatus)) {
                deviceState = FvsDeviceStatus.ONLINE.getValue();
            } else if (FvsCallbackStatus.OFFLINE.value().equals(deviceStatus)) {
                deviceState = FvsDeviceStatus.OFFLINE.getValue();
            } else if (FvsCallbackStatus.FAIL.value().equals(deviceStatus)) {
                deviceState = FvsDeviceStatus.FAIL.getValue();
                FvsDeviceDTO fvsDeviceDTO = fvsDeviceService.selectByAliDeviceId(deviceId);
                //删除设备
                fvsDeviceService.delDevice(fvsDeviceDTO.getId());
            }
            fvsDeviceService.updateByAliDeviceId(deviceId, deviceState, null);
        }
    }

    @Override
    public void rtmpGroupCallback(String body) {
        FvsGroupCallbackParam fvsGroupCallbackParam = JSON.parseObject(body, FvsGroupCallbackParam.class);
        String streamStatus = fvsGroupCallbackParam.getStreamStatus();
        String event = fvsGroupCallbackParam.getEvent();
        String streamId = fvsGroupCallbackParam.getStreamId();
        if (FvsCallbackType.STREAM.value().equals(event)) {
            Integer deviceState = null;
            Integer streamState = 0;
            if (FvsCallbackStatus.ONLINE.value().equals(streamStatus)) {
                streamState = FvsStreamStatus.ONLINE.getValue();
                deviceState = FvsDeviceStatus.ONLINE.getValue();
            }
            if (FvsCallbackStatus.OFFLINE.value().equals(streamStatus)) {
                streamState = FvsStreamStatus.OFFLINE.getValue();
            }
            fvsDeviceService.updateByAliStreamId(streamId, deviceState, streamState, null);
        }
    }

    /**
     * 视频截图回调
     */
    @Override
    public void snapshotCallback(String body) {
        FvsSnapshotCallbackParam parseObject = JSON.parseObject(body, FvsSnapshotCallbackParam.class);
        String gbId = parseObject.getStreamName();
        String snapshotUrl = parseObject.getSnapshotUrl();
        if (StringUtils.isNoneBlank(gbId, snapshotUrl)) {
            fvsDeviceService.updateSnapshotUrlByGbId(gbId, snapshotUrl);
        }
    }

    @Override
    public void rtmpSnapshotCallback(String body) {
        FvsSnapshotCallbackParam parseObject = JSON.parseObject(body, FvsSnapshotCallbackParam.class);
        AliyunStreamInfoDTO streamInfo = fvsDeviceService.getAliStreamInfo(parseObject.getAppName(), parseObject.getStreamName());
        if (streamInfo != null) {
            String streamId = streamInfo.getId();
            if (!StringUtils.isEmpty(streamId)) {
                fvsDeviceService.updateByAliStreamId(streamId, null, null, parseObject.getSnapshotUrl());
            }
        }
    }

    /**
     * 视频录制回调
     */
    @Override
    public void recordCallback(String body) {
        FvsRecordCallbackParam parseObject = JSON.parseObject(body, FvsRecordCallbackParam.class);
        String fileFormat = "flv";
        String url = parseObject.getUri();
        long startTime = parseObject.getStart_time() * 1000;
        long stopTime = parseObject.getStop_time() * 1000;
        String stream = parseObject.getStream();

        FvsDeviceDTO fvsDeviceDTO = fvsDeviceService.selectByGbId(stream);
        Integer deviceId = fvsDeviceDTO.getId();

        FvsRecordDTO fvsRecordDTO = new FvsRecordDTO();
        fvsRecordDTO.setDeviceId(deviceId);
        fvsRecordDTO.setEndTime(new Date(stopTime));
        fvsRecordDTO.setFileFormat(fileFormat);
        fvsRecordDTO.setRecordUrl(ossFvsUrl + "/" + url);
        fvsRecordDTO.setStartTime(new Date(startTime));
        fvsRecordService.insertRecord(fvsRecordDTO);
    }

    @Override
    public void rtmpRecordCallback(String body) {
        FvsRecordCallbackParam parseObject = JSON.parseObject(body, FvsRecordCallbackParam.class);
        String fileFormat = "flv";
        String url = parseObject.getUri();
        long startTime = parseObject.getStart_time() * 1000;
        long stopTime = parseObject.getStop_time() * 1000;
        AliyunStreamInfoDTO streamInfo = fvsDeviceService.getAliStreamInfo(parseObject.getApp(), parseObject.getStream());
        if (streamInfo == null) {
            return;
        }
        String streamId = streamInfo.getId();
        FvsDeviceDTO fvsDeviceDTO = fvsDeviceService.selectByAliStreamId(streamId);
        if (fvsDeviceDTO == null) {
            return;
        }
        Integer deviceId = fvsDeviceDTO.getId();
        FvsRecordDTO fvsRecordDTO = new FvsRecordDTO();
        fvsRecordDTO.setDeviceId(deviceId);
        fvsRecordDTO.setEndTime(new Date(stopTime));
        fvsRecordDTO.setFileFormat(fileFormat);
        fvsRecordDTO.setRecordUrl(ossFvsUrl + "/" + url);
        fvsRecordDTO.setStartTime(new Date(startTime));
        fvsRecordService.insertRecord(fvsRecordDTO);

    }

    @Override
    public void dealBetvCallback(String body) {
        JSONArray dataArray = JSONArray.parseArray(body);
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject jsonObject = dataArray.getJSONObject(i);
            BetvDtoData betvDtoData = jsonObject.toJavaObject(BetvDtoData.class);
            logger.info("倍特威视转换后的数据:"+i+"\\n"+ betvDtoData.toString());
        }
    }

}
